package com.zixi.ai.framework.dao.spec;

import com.zixi.ai.common.utils.StringUtils;

import java.util.Collection;


/**
 * @author mqiqe
 */
public class Restrictions {
    /**
     * FUN_YEAR
     */
    public static FunctionExpression year(String fieldName, Object value) {
        return new FunctionExpression(fieldName, value, Criterion.Operator.FUN_YEAR);
    }

    /**
     * FUN_MONTH
     */
    public static FunctionExpression month(String fieldName, Object value) {
        return new FunctionExpression(fieldName, value, Criterion.Operator.FUN_MONTH);
    }

    /**
     * isEmpty
     */
    public static SimpleExpression isEmpty(String fieldName) {
        return new SimpleExpression(fieldName, null, Criterion.Operator.ISEMPTY);
    }

    /**
     * isNotEmpty
     */
    public static SimpleExpression isNotEmpty(String fieldName) {
        return new SimpleExpression(fieldName, null, Criterion.Operator.ISNOTEMPTY);
    }

    /**
     * isnull
     */
    public static SimpleExpression isNull(String fieldName) {
        return new SimpleExpression(fieldName, null, Criterion.Operator.ISNULL);
    }

    /**
     * isnotnull
     */
    public static SimpleExpression isNotNull(String fieldName) {
        return new SimpleExpression(fieldName, null, Criterion.Operator.ISNOTNULL);
    }

    /**
     * 等于
     */
    public static SimpleExpression eq(String fieldName, Object value) {
        if (value == null || StringUtils.isBlank(value.toString())) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.EQ);
    }

    /**
     * 不等于
     */
    public static SimpleExpression ne(String fieldName, Object value) {
        if (value == null) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.NE);
    }

    /**
     * 模糊匹配
     */
    public static SimpleExpression like(String fieldName, String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.LIKE);
    }

    /**
     * 模糊匹配 取反
     */
    public static SimpleExpression nlike(String fieldName, String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.NLIKE);
    }

    /**
     * 模糊匹配
     */
    public static SimpleExpression likeCustom(String fieldName, String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.LIKE_CUSTOM);
    }

    /**
     * 大于
     */
    public static SimpleExpression gt(String fieldName, Object value) {
        if (value == null) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.GT);
    }

    /**
     * 小于
     */
    public static SimpleExpression lt(String fieldName, Object value) {
        if (value == null) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.LT);
    }

    /**
     * 小于 等于
     */
    public static SimpleExpression lte(String fieldName, Object value) {
        if (value == null) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.LTE);
    }

    /**
     * 大于等于
     */
    public static SimpleExpression gte(String fieldName, Object value) {
        if (value == null) {
            return null;
        }
        return new SimpleExpression(fieldName, value, Criterion.Operator.GTE);
    }

    /**
     * 并且
     */
    public static LogicalExpression and(Criterion... criterions) {
        return new LogicalExpression(criterions, Criterion.Operator.AND);
    }

    /**
     * 或者
     */
    public static LogicalExpression or(Criterion... criterions) {
        return new LogicalExpression(criterions, Criterion.Operator.OR);
    }

    /**
     * 包含于
     */
    @SuppressWarnings("rawtypes")
    public static LogicalExpression in(String fieldName, Collection value, boolean ignoreNull) {
        if (ignoreNull && (value == null || value.isEmpty())) {
            return null;
        }
        SimpleExpression[] ses = new SimpleExpression[value.size()];
        int i = 0;
        for (Object obj : value) {
            ses[i] = new SimpleExpression(fieldName, obj, Criterion.Operator.EQ);
            i++;
        }
        return new LogicalExpression(ses, Criterion.Operator.OR);
    }

    /**
     * 不包含于
     */
    @SuppressWarnings("rawtypes")
    public static LogicalExpression notIn(String fieldName, Collection value, boolean ignoreNull) {
        if (ignoreNull && (value == null || value.isEmpty())) {
            return null;
        }
        SimpleExpression[] ses = new SimpleExpression[value.size()];
        int i = 0;
        for (Object obj : value) {
            ses[i] = new SimpleExpression(fieldName, obj, Criterion.Operator.NE);
            i++;
        }
        return new LogicalExpression(ses, Criterion.Operator.AND);
    }
}  